package com.wafersystems.springbootdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步任务线程池配置
 *
 * @author tandk
 * @date 2018/12/5 17:45
 */
@EnableAsync
@Configuration
public class TaskExecutorConfig {

    /**
     * 线程池维护线程的最小数量
     */
    private final static int CORE_POOL_SIZE = 10;

    /**
     * 最大线程数
     */
    private final static int MAX_POOL_SIZE = 200;

    /**
     * 队列容量
     */
    private final static int QUEUE_CAPACITY = 100;

    @Bean
    public Executor demoAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix("demoExecutor-");
        executor.initialize();
        return executor;
    }
}
